/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-05-28 12:10:37
 * @LastEditTime: 2022-06-20 17:21:01
 */

import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import {
  ADD_DICTIONARY,
  ADD_DICTIONARY_ITEM,
  DELETE_DICTIONARY,
  DELETE_DICTIONARY_ITEM,
  EDIT_DICTIONARY_ITEM,
  SET_DICTIONARY_ITEM_LIST,
  SET_DICTIONARY_LIST,
  SET_SELECTED_ITEM,
  SET_SELECT_AT,
  SET_SELECT_DICTIONARY,
  SET_SELECT_FOND,
  EDIT_DICTIONARY
} from '../constants';

export const DictionaryReducer = (state, action) => {
  switch (action.type) {
    // 设置选中的全宗
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    // 设置选中的档案类型
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };
    // 设置选中的字典
    case SET_SELECT_DICTIONARY:
      return {
        ...state,
        selectDictionary: action.data
      };

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @return {*}
     */
    case SET_DICTIONARY_LIST: {
      return {
        ...state,
        dictionaryList: action.data
      };
    }

    /**
     * @description: 添加字典
     * @param {Array} data 添加的字典
     * @return {*}
     */
    case ADD_DICTIONARY: {
      return {
        ...state,
        dictionaryList: insertItem(action.data, state.dictionaryList)
      };
    }

    /**
     * @description: 添加字典
     * @param {Array} data 添加的字典
     * @return {*}
     */
    case EDIT_DICTIONARY: {
      return {
        ...state,
        dictionaryList: editItemById(action.data.id, action.data, state.dictionaryList)
      };
    }

    /**
     * @description: 删除字典
     * @param {number[]} ids 删除的字典ID
     */
    case DELETE_DICTIONARY:
      return {
        ...state,
        dictionaryList: delItemByIdArray(action.ids, state.dictionaryList)
      };

    /**
     * @description: 表格选中的项目
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 加载字典项目列表
     * @param {Array} data 表格数据
     */
    case SET_DICTIONARY_ITEM_LIST: {
      return {
        ...state,
        dictionaryItemList: action.data
      };
    }

    /**
     * @description: 添加字典项目
     * @param {Array} data 添加的字典项
     */
    case ADD_DICTIONARY_ITEM: {
      return {
        ...state,
        dictionaryItemList: insertItem(action.data, state.dictionaryItemList)
      };
    }

    /**
     * @description: 编辑字典项目
     * @param {Array} data 编辑的字典项
     */
    case EDIT_DICTIONARY_ITEM: {
      return {
        ...state,
        dictionaryItemList: editItemById(action.id, action.data, state.dictionaryItemList)
      };
    }

    /**
     * @description: 删除字典项目
     * @param {number[]} ids 删除的字典ID
     */
    case DELETE_DICTIONARY_ITEM:
      return {
        ...state,
        dictionaryItemList: delItemByIdArray(action.ids, state.dictionaryItemList)
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
